<?php
/**
 * BaseRepository
 * @copyright (c) 2011-2020, Hangzhou DOJI Tech Co., Ltd.
 * This is NOT a freeware, use is subject to license terms.
 * @package BaseRepository.php
 * @link https://www.tingjunapp.com/
 * @author wujun@lanqb.cn
 * @: BaseRepository.php 310000 2020-11-06 15:07:29 $
 * */

namespace App\Repository;

use App\Consts\ExceptionCode;
use App\Services\BaseService;
use App\Services\ErrorService;
use Illuminate\Support\Facades\Validator;

class BaseRepository extends Repository
{

    /** @var null $validator */
    public $validator = null;

    /**
     * search filed
     * @param $params
     * @return array|mixed
     */
    public function search($params)
    {
        // search
        return [];
    }

    /**
     * @param array $data
     * @param array $rule
     * @param array $message
     * @return bool
     * @throws \Exception
     */
    public function validate($data = [], $rule = [], $message = [])
    {
        $this->validator = Validator::make($data, $rule, $message);
        if ($this->validator->fails()) {
            return $this->throwError(ExceptionCode::ILLEGAL_PARAMETER, $this->validator->errors()->first());
        }
        return true;
    }

    /**
     * 异常抛出
     * @param int $code
     * @param string $message
     * @return bool
     * @throws \Exception
     */
    public function throwError($code = ExceptionCode::ILLEGAL_PARAMETER, $message = '')
    {
        return ErrorService::throwError($code, $message);
    }

}
